package com.snail.readyvr.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.snail.readyvr.R;
import com.snail.readyvr.fragment.DownloadedFragment;
import com.snail.readyvr.fragment.DownloadingFragment;
import com.snail.readyvr.ui.CommonTopBar;
import com.snail.readyvr.ui.IndicatorTiTle;

import butterknife.BindArray;
import butterknife.BindView;

/**
 * Created by MRKING on 2017/1/9.
 */

public class DownloadManagerActivity extends CheckPermissionsActivity implements CommonTopBar.OnClickCommonTopBar,IndicatorTiTle.OnClickTitle{
    @BindView(R.id.download_top_bar)
    CommonTopBar mDownloadTopBar;
    @BindView(R.id.download_indicator_title)
    IndicatorTiTle mDownloadIndicatorTitle;
    @BindView(R.id.download_content_layout)
    FrameLayout mDownloadContentLayout;

    @BindArray(R.array.download_titles)
    String[] mTitles;

    private DownloadedFragment mDownloadedFragment;
    private DownloadingFragment mDownloadingFragment;

    @Override
    protected void hasGetAllPermissions() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_download_manager;
    }

    @Override
    protected void init() {
        mDownloadTopBar.setOnClickCommonTopBar(this);
        mDownloadTopBar.isShowRightView(false);
        mDownloadTopBar.midTextView.setText(getString(R.string.download_title));
        mDownloadTopBar.mRightTextView.setText(getString(R.string.download_right_title));
        mDownloadIndicatorTitle.setOnClickTitle(this);
        mDownloadIndicatorTitle.setTitls(mTitles);
        mDownloadedFragment = new DownloadedFragment();
        mDownloadingFragment = new DownloadingFragment();
        FragmentTransaction ft = getFragmentTransaction();
        ft.add(R.id.download_content_layout, mDownloadedFragment);
        ft.add(R.id.download_content_layout, mDownloadingFragment);
        ft.commit();
        changeFragment(0);
    }

    @Override
    public void onClickLeftView() {
      finish();
    }

    @Override
    public void onClickRightView() {

    }

    @Override
    public void onClickTitle(int position) {
        changeFragment(position);
    }

    private void changeFragment (int position){
        FragmentTransaction ft = getFragmentTransaction();
        ft.show(position == 0?mDownloadedFragment:mDownloadingFragment);
        ft.hide(position == 1?mDownloadedFragment:mDownloadingFragment);
        ft.commit();
    }

    private FragmentTransaction getFragmentTransaction() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        //ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        return ft;
    }
}
